#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
//服务器端
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString ip=GetLocalIP();
    ui->Severaddress->addItem(ip);

    socket=NULL;
    tcpserver=NULL;
    tcpserver=new QTcpServer(this);

    connect(tcpserver,&QTcpServer::newConnection,this,&MainWindow::newconnection1);

}

MainWindow::~MainWindow()
{
    delete ui;
}

QString  MainWindow::GetLocalIP()
{
    QString str="";
    QString hostname=QHostInfo::localHostName();
    //QHostInfo fromName(const QString &name)
    QHostInfo Ipaddress=QHostInfo::fromName(hostname);
    //QList<QHostAddress> QHostInfo::addresses() const
    QList<QHostAddress>list=Ipaddress.addresses();
    if(!list.isEmpty())
    {
        for(QHostAddress address:list)
        {
            qDebug()<<address.toString();
            if(address.protocol()==QAbstractSocket::IPv4Protocol)
            {
                str=address.toString();
                break;
            }
        }
    }

    return str;
}


void MainWindow::clientConnect()
{
    ui->ReceverText->appendPlainText("**********客户端socket连接**********");
    ui->ReceverText->appendPlainText("**********peer address:"+socket->peerAddress().toString());
    ui->ReceverText->appendPlainText("**********peer Port:"+(QString::number(socket->peerPort())));

}

void MainWindow::clienxtDisConnect()
{
    ui->ReceverText->appendPlainText("**********客户端socket断开连接**********");
    socket->deleteLater();
}

void MainWindow::newconnection1()
{
    socket=tcpserver->nextPendingConnection();
    connect(socket,&QTcpSocket::connected,this,&MainWindow::clientConnect);
    clientConnect();
    connect(socket,&QTcpSocket::disconnected,this,clienxtDisConnect);
    //都客户端发来的数据
    connect(socket,&QTcpSocket::readyRead,this,&MainWindow::ReadClientMessage);

// connect(socket, &QTcpSocket::stateChanged, this, &MainWindow::OnSocketStateChanged);
}

void MainWindow::ReadClientMessage()
{
   while(socket->canReadLine())
   {
       ui->ReceverText->appendPlainText("[in]:"+socket->readLine());
   }
}
//启动服务器
void MainWindow::on_StartServerbtn_clicked()
{

  QString Ip=ui->Severaddress->currentText();
  qint16 port=ui->ServerPort->value();
  QHostAddress address(Ip);
  tcpserver->listen(address,port);
  ui->ReceverText->appendPlainText("$$$$$$$$$$开始监听$$$$$$$$$$");
  ui->ReceverText->appendPlainText("$$$$$$$$$$服务器地址$$$$$$$$$$"+tcpserver->serverAddress().toString());
  ui->ReceverText->appendPlainText("$$$$$$$$$$服务器端口$$$$$$$$$$"+QString::number(tcpserver->serverPort()));
  ui->StartServerbtn->setDisabled(true);
  ui->StopServerbtn->setDisabled(false);

}

//停止服务器
void MainWindow::on_StopServerbtn_clicked()
{
    if(tcpserver->isListening())
    {
        tcpserver->close();
        ui->StartServerbtn->setEnabled(true);
        ui->StopServerbtn->setEnabled(false);
    }


}

//服务器向客户端发送消息
void MainWindow::on_SendMessageBtn_clicked()
{
   QString text= ui->MessageLine->text();
   ui->MessageLine->clear();
   ui->ReceverText->appendPlainText("[out]:"+text);
   QByteArray str=text.toUtf8();
   str.append("\n");
   socket->write(str);
}
